package cn.kgc.tushu.serivce;

import java.io.*;
import java.util.Map;

public class Opera {
    /**
     * 序列化
     * @param map
     * @param file
     */
    public static void Seria(Map map, File file){
        ObjectOutputStream ost=null;
        if (file.exists()){
            try {
                ost=new ObjectOutputStream(new FileOutputStream(file));
                ost.writeObject(map);
                ost.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (ost!=null){
                    try {
                        ost.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 反序列化
     * @param file
     * @return
     */
    public static Map unSeria(File file){
        ObjectInputStream ois = null;
        Map map=null;
        try {
            ois=new ObjectInputStream(new FileInputStream(file));
            map= (Map) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }
}
